﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Data;
using LDSP.BRL;

namespace LDSP.Util
{
    public class ChemicalExport
    {
        private IDictionary<int, ChemicalCatalog> _catalogDic = InitailCatalogDic();

        private static IDictionary<int, ChemicalCatalog> InitailCatalogDic()
        {
            IDictionary<int, ChemicalCatalog> catalogDic = new Dictionary<int, ChemicalCatalog>();
            DataTable dt = new ChemicalCatalog().GetAll();

            foreach (DataRow row in dt.Rows)
            {
                ChemicalCatalog catalog = new ChemicalCatalog();
                catalog.GetProperties(row);
                catalogDic.Add(catalog.ID, catalog);
            }

            return catalogDic;
        }

        private void Export(HttpServerUtility server, DataTable source)
        {
            ExcelClass excel = new ExcelClass(server.MapPath("..\\..\\..\\Templates\\Chemical.xls"));
            excel.ImportDataTable(excel.GetSheetByIndex(0), false, GetExportedDT(source), 3, 1);
            excel.ToExcel(@"E:\Lab\Web\ExcelImportExport\Chemical.xls", DownloadType.PromptDialog);
        }

        public void Export(HttpServerUtility server, IList<Chemical> chemicals)
        {
            ExcelClass excel = new ExcelClass(server.MapPath("..\\..\\..\\Templates\\chemicalImport.xls"));
            excel.ImportDataTable(excel.GetSheetByIndex(0), false, GetExportedDT(chemicals), 2, 1);
            excel.ToExcel(@"E:\Lab\Web\ExcelImportExport\ChemicalList.xls", DownloadType.PromptDialog);
        }

        private DataTable GetExportedDT(DataTable source)
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("Sequence");
            dt.Columns.Add("Name");
            dt.Columns.Add("Alias");
            dt.Columns.Add("CASNumber");
            dt.Columns.Add("UNNumber");
            dt.Columns.Add("Catalog");
            dt.Columns.Add("DangousNumber");
            dt.Columns.Add("CustomNumber");
            dt.Columns.Add("DangousType");
            dt.Columns.Add("PackageType");
            dt.Columns.Add("Unit");
            dt.Columns.Add("LimitedScope");
            dt.Columns.Add("TransportationMethod");
            dt.Columns.Add("EnglishName");
            dt.Columns.Add("TypeCatalog");
            dt.Columns.Add("SecondaryDangerous");
            dt.Columns.Add("ExceptionNumber");
            dt.Columns.Add("LimitedNumber");

            int index = 1;
            foreach (DataRow row in source.Rows)
            {
                int catalogID = row["catalog"] != DBNull.Value ? Int32.Parse(row["catalog"].ToString()) : -1;
                dt.Rows.Add(index,
                    row["name"],
                    row["alias"],
                    row["CAS"],
                    row["UN"],
                    _catalogDic.ContainsKey(catalogID) ? _catalogDic[catalogID].Name : "",
                    row["dangerNumber"],
                    row["goodsNumber"],
                    row["dangerType"],
                    row["packageType"],
                    row["unit"],
                    row["limitedScope"],
                    row["transportationMethod"],
                    row["englishName"],
                    row["typeCatalog"],
                    row["secondaryDangerous"],
                    row["exceptionNumber"],
                    row["limitationNumber"]
                    );

                index++;
            }

            return dt;
        }

        private DataTable GetExportedDT(IList<Chemical> chemicals)
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("Sequence");
            dt.Columns.Add("Name");
            dt.Columns.Add("Alias");
            dt.Columns.Add("CASNumber");
            dt.Columns.Add("UNNumber");
            dt.Columns.Add("Catalog");
            dt.Columns.Add("DangousNumber");
            dt.Columns.Add("CustomNumber");
            dt.Columns.Add("DangousType");
            dt.Columns.Add("PackageType");
            dt.Columns.Add("Unit");
            dt.Columns.Add("LimitedScope");
            dt.Columns.Add("TransportationMethod");
            dt.Columns.Add("EnglishName");
            dt.Columns.Add("TypeCatalog");
            dt.Columns.Add("SecondaryDangerous");
            dt.Columns.Add("ExceptionNumber");
            dt.Columns.Add("LimitedNumber");

            int index = 1;
            foreach (Chemical ch in chemicals)
            {
                int catalogID = !string.IsNullOrEmpty(ch.Catalog) ? Int32.Parse(ch.Catalog) : -1;
                dt.Rows.Add(index,
                    ch.Name,
                    ch.Alias,
                    ch.CAS,
                    ch.UN,
                    _catalogDic.ContainsKey(catalogID) ? _catalogDic[catalogID].Name : "",
                    ch.DangerNumber,
                    ch.GoodsNumber,
                    ch.DangerType,
                    ch.PackageType,
                    ch.Unit,
                    ch.LimitedScope,
                    ch.TransportMethod,
                    ch.EnglishName,
                    ch.TypeCatalog,
                    ch.SecondaryDangerous,
                    ch.ExceptionNumber,
                    ch.LimitationNumber);

                index++;
            }

            return dt;
        }
    }
}
